import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:demo/providers/counter_providers.dart';

void main() {
  group('Riverpod Providers Tests', () {
    late ProviderContainer container;

    setUp(() {
      container = ProviderContainer();
    });

    tearDown(() {
      container.dispose();
    });

    group('CounterState Tests', () {
      test('初始状态应该正确', () {
        const state = CounterState();
        expect(state.count, 0);
        expect(state.username, 'Guest');
        expect(state.isLoading, false);
        expect(state.status, 'Zero');
      });

      test('copyWith应该正确创建新状态', () {
        const state = CounterState();
        final newState = state.copyWith(count: 5, username: 'Alice');
        
        expect(newState.count, 5);
        expect(newState.username, 'Alice');
        expect(newState.isLoading, false); // 未改变的值应该保持
        expect(newState.status, 'Zero'); // 未改变的值应该保持
      });

      test('counterStatus应该根据计数值返回正确状态', () {
        expect(const CounterState(count: 0).counterStatus, 'Zero');
        expect(const CounterState(count: 5).counterStatus, 'Low');
        expect(const CounterState(count: 25).counterStatus, 'Medium');
        expect(const CounterState(count: 100).counterStatus, 'High');
      });

      test('相等性比较应该正确工作', () {
        const state1 = CounterState(count: 5, username: 'Alice');
        const state2 = CounterState(count: 5, username: 'Alice');
        const state3 = CounterState(count: 10, username: 'Alice');

        expect(state1, equals(state2));
        expect(state1, isNot(equals(state3)));
      });
    });

    group('CounterNotifier Tests', () {
      test('初始状态应该正确', () {
        final state = container.read(counterProvider);
        expect(state.count, 0);
        expect(state.username, 'Guest');
        expect(state.isLoading, false);
        expect(state.status, 'Zero');
      });

      test('increment应该增加计数器', () {
        final notifier = container.read(counterProvider.notifier);
        
        notifier.increment();
        final state = container.read(counterProvider);
        
        expect(state.count, 1);
        expect(state.status, 'Low');
      });

      test('decrement应该减少计数器', () {
        final notifier = container.read(counterProvider.notifier);
        
        // 先增加一些值
        notifier.addMultiple(5);
        notifier.decrement();
        
        final state = container.read(counterProvider);
        expect(state.count, 4);
        expect(state.status, 'Low');
      });

      test('decrement不应该让计数器变为负数', () {
        final notifier = container.read(counterProvider.notifier);
        
        notifier.decrement(); // 从0开始减少
        
        final state = container.read(counterProvider);
        expect(state.count, 0); // 应该保持为0
      });

      test('reset应该重置计数器', () {
        final notifier = container.read(counterProvider.notifier);
        
        notifier.addMultiple(10);
        notifier.reset();
        
        final state = container.read(counterProvider);
        expect(state.count, 0);
        expect(state.status, 'Zero');
      });

      test('setUsername应该更新用户名', () {
        final notifier = container.read(counterProvider.notifier);
        
        notifier.setUsername('Alice');
        
        final state = container.read(counterProvider);
        expect(state.username, 'Alice');
      });

      test('addMultiple应该批量增加计数器', () {
        final notifier = container.read(counterProvider.notifier);
        
        notifier.addMultiple(15);
        
        final state = container.read(counterProvider);
        expect(state.count, 15);
        expect(state.status, 'Medium');
      });

      test('setCount应该设置指定值', () {
        final notifier = container.read(counterProvider.notifier);
        
        notifier.setCount(50);
        
        final state = container.read(counterProvider);
        expect(state.count, 50);
        expect(state.status, 'High');
      });

      test('incrementAsync应该异步增加计数器', () async {
        final notifier = container.read(counterProvider.notifier);
        
        // 开始异步操作
        final future = notifier.incrementAsync();
        
        // 应该立即设置为加载状态
        expect(container.read(counterProvider).isLoading, true);
        expect(container.read(counterProvider).count, 0);
        
        // 等待异步操作完成
        await future;
        
        // 操作完成后
        final state = container.read(counterProvider);
        expect(state.isLoading, false);
        expect(state.count, 1);
        expect(state.status, 'Low');
      });
    });

    group('Derived Providers Tests', () {
      test('counterValueProvider应该返回正确的计数值', () {
        final notifier = container.read(counterProvider.notifier);
        notifier.addMultiple(25);
        
        final count = container.read(counterValueProvider);
        expect(count, 25);
      });

      test('usernameProvider应该返回正确的用户名', () {
        final notifier = container.read(counterProvider.notifier);
        notifier.setUsername('Bob');
        
        final username = container.read(usernameProvider);
        expect(username, 'Bob');
      });

      test('statusProvider应该返回正确的状态', () {
        final notifier = container.read(counterProvider.notifier);
        notifier.addMultiple(30);
        
        final status = container.read(statusProvider);
        expect(status, 'Medium');
      });

      test('isLoadingProvider应该返回正确的加载状态', () {
        final isLoading = container.read(isLoadingProvider);
        expect(isLoading, false);
      });

      test('isEvenProvider应该正确判断奇偶性', () {
        final notifier = container.read(counterProvider.notifier);
        
        // 偶数
        notifier.setCount(4);
        expect(container.read(isEvenProvider), true);
        
        // 奇数
        notifier.setCount(5);
        expect(container.read(isEvenProvider), false);
      });

      test('nextMilestoneProvider应该返回正确的里程碑信息', () {
        final notifier = container.read(counterProvider.notifier);
        
        notifier.setCount(5);
        final milestone = container.read(nextMilestoneProvider);
        expect(milestone, '5 to reach 10');
        
        notifier.setCount(1500);
        final milestone2 = container.read(nextMilestoneProvider);
        expect(milestone2, 'You\'ve reached all milestones!');
      });

      test('progressProviders应该返回正确的进度', () {
        final notifier = container.read(counterProvider.notifier);
        
        notifier.setCount(50);
        
        expect(container.read(progressTo100Provider), 0.5);
        expect(container.read(progressTo500Provider), 0.1);
        expect(container.read(progressTo1000Provider), 0.05);
        
        // 测试超过目标值的情况
        notifier.setCount(150);
        expect(container.read(progressTo100Provider), 1.0);
      });
    });

    group('SettingsNotifier Tests', () {
      test('初始设置应该正确', () {
        final settings = container.read(settingsProvider);
        expect(settings['theme'], 'light');
        expect(settings['notifications'], true);
        expect(settings['autoSave'], false);
      });

      test('updateSetting应该更新设置', () {
        final notifier = container.read(settingsProvider.notifier);
        
        notifier.updateSetting('theme', 'dark');
        notifier.updateSetting('notifications', false);
        
        final settings = container.read(settingsProvider);
        expect(settings['theme'], 'dark');
        expect(settings['notifications'], false);
        expect(settings['autoSave'], false); // 未改变的值应该保持
      });

      test('resetSettings应该重置所有设置', () {
        final notifier = container.read(settingsProvider.notifier);
        
        // 先修改一些设置
        notifier.updateSetting('theme', 'dark');
        notifier.updateSetting('notifications', false);
        notifier.updateSetting('autoSave', true);
        
        // 然后重置
        notifier.resetSettings();
        
        final settings = container.read(settingsProvider);
        expect(settings['theme'], 'light');
        expect(settings['notifications'], true);
        expect(settings['autoSave'], false);
      });

      test('themeProvider应该返回正确的主题', () {
        final notifier = container.read(settingsProvider.notifier);
        
        notifier.updateSetting('theme', 'dark');
        
        final theme = container.read(themeProvider);
        expect(theme, 'dark');
      });
    });

    group('Provider Dependencies Tests', () {
      test('派生Provider应该在依赖变化时更新', () {
        final notifier = container.read(counterProvider.notifier);
        
        // 监听派生状态
        var count = container.read(counterValueProvider);
        var status = container.read(statusProvider);
        var isEven = container.read(isEvenProvider);
        
        expect(count, 0);
        expect(status, 'Zero');
        expect(isEven, true);
        
        // 改变主状态
        notifier.addMultiple(15);
        
        // 派生状态应该自动更新
        count = container.read(counterValueProvider);
        status = container.read(statusProvider);
        isEven = container.read(isEvenProvider);
        
        expect(count, 15);
        expect(status, 'Medium');
        expect(isEven, false);
      });
    });
  });
}
